package com.sita.bluetactivity.LoginAndRegister;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sita.bluetactivity.Base.BaseActivity;
import com.sita.bluetactivity.Base.BaseApplication;
import com.sita.bluetactivity.Base.SpUtils;
import com.sita.bluetactivity.Base.ToastUtils;
import com.sita.bluetactivity.MainActivity;
import com.sita.bluetactivity.R;
import com.sita.bluetactivity.WelcomeActivity;
import com.sita.bluetactivity.http.RestClient;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/10/17.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private EditText loginPhone,loginPass;
    private RelativeLayout registerLayout;
    private Button login_btn;
    private ProgressDialog dialog;
    private LinearLayout phoneLayout,passLayout;
    private TextView forget_Tx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initData() {
        login_btn.setOnClickListener(this);
        registerLayout.setOnClickListener(this);
    }

    @Override
    protected void initView() {
        loginPhone = bindView(R.id.login_phone);
        loginPass = bindView(R.id.login_pass);
        registerLayout = bindView(R.id.jump_register);
        login_btn = bindView(R.id.login_bt);
        phoneLayout = bindView(R.id.phone_layout);
        forget_Tx = bindView(R.id.forget_tx);
        forget_Tx.setOnClickListener(this);
        passLayout = bindView(R.id.pass_layout);
        loginPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    passLayout.setBackground(getDrawable(R.drawable.frame_style));
                }
                else {
                    passLayout.setBackground(getDrawable(R.drawable.frame_nofocus_style));
                }
            }
        });
        loginPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    phoneLayout.setBackground(getDrawable(R.drawable.frame_style));
                }
                else {
                    phoneLayout.setBackground(getDrawable(R.drawable.frame_nofocus_style));
                }
            }
        });
    }

    @Override
    protected int getId() {
        boolean isFirst =  SpUtils.getBoolean("isFirst",true, BaseApplication.getContext());
        if (isFirst){

        }else {
            WelcomeActivity.jumpWelcomeIntent(LoginActivity.this);
            finish();
        }
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.jump_register:
            RegisterActivity.jumpRegister(this);
                break;
            case R.id.login_bt:
                loginCheck();
                break;
            case R.id.forget_tx:
                ForgetPassActivity.jumpForget(this);
                break;
        }
    }
    private void loginCheck(){
        String phone = loginPhone.getText().toString();
        String pass = loginPass.getText().toString();
        if (phone.length()==0||pass.length()==0){
            ToastUtils.show("输入有误,请重新输入.",2000);
        }
        else {
            dialog = ProgressDialog.show(this,null,"登录中...");
            //调用接口
            LoginRequest request = new LoginRequest();
            request.mobile = loginPhone.getText().toString();
            request.password = loginPass.getText().toString();
            RestClient.getRestService().login(request, new Callback<LoginBackBean>() {
                @Override
                public void success(LoginBackBean loginBackBean, Response response) {
                    if (loginBackBean.getErrorCode().equals("0")&&loginBackBean.getData()!=null){
                        SpUtils.putBoolean("isFirst",false, BaseApplication.getContext());//记录是否为第一次登录
                        SpUtils.putString("AccountId",loginBackBean.getData().getAccount().getAccountId(),BaseApplication.getContext());
                        SpUtils.putString("UserIcon",loginBackBean.getData().getAccount().getAvatar(),BaseApplication.getContext());
                        SpUtils.putString("UserNickName",loginBackBean.getData().getAccount().getNickName(),BaseApplication.getContext());
                        SpUtils.putString("UserPhone",loginBackBean.getData().getAccount().getMobile(),BaseApplication.getContext());
                        dialog.dismiss();//登录成功
                        MainActivity.getMainIntent(LoginActivity.this);
                        finish();
                    }
                }

                @Override
                public void failure(RetrofitError error) {
                    dialog.dismiss();
                    ToastUtils.show("登录失败",3000);
                }
            });

        }
    }

    public static void jumpLoginIntent(Context context){
        Intent intent = new Intent(context,LoginActivity.class);
        context.startActivity(intent);
    }
}
